<?php
/**
 * @author yuanyitian
 * @date 2024/10/19 13:45
 */

namespace api\wechat;

use utils\base64_to_file;
use utils\oss;

class task
{
    //任务上传
    public function up($data){
        $con=con();
        $openid=$data['openid'];
        $phone=(int)$data['phone'];
        $pre_time=strtotime($data['pre_time']);
        $content=$data['content'];
        $room_id=$data['room_id'];
        $room_name=$data['room_name'];
        $img=$data['img'];
        $date=date('Y-m-d');
        $url=[];

        $user_id=mysqli_query($con,"select * from user where openid='$openid' ");
        $user_id=mysqli_fetch_all($user_id,true);
        if(count($user_id)==0){
            mysqli_close($con);
            return out_error('查无此人！');
        }
        $user_id=(int)$user_id[0]['id'];
        //权限控制
        $res=mysqli_query($con,"select * from user_power_config where status=1 and is_delete=1 and user_id=$user_id and type=2 and check_status=1");
        $res=mysqli_fetch_all($res,true);
        if(count($res)==0){
            mysqli_close($con);
            return out_error('此用户无权限！');
        }

        for($i=0;$i<count($img);$i++){
            $file=base64_to_file::get("",$img[$i]["file_name"],$img[$i]["file"]);
            $res=oss::upload("task/$date/".$file["name"],$file["path"]);
            $url[]=$res["info"]["url"];
        }
        $url=implode(',',$url);
        $time=time();
        $res=mysqli_query($con,"insert into task(content,img_url,up_user_id,pre_time,create_time,update_time,room_id,room_name) values('$content','$url',$user_id,$pre_time,$time,$time,$room_id,'$room_name')");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error('任务发布失败！');
        }
    }

    //任务分页列表
    public function getList($data){
        $con=con();
        if(isset($data['user_id'])){
            $user_id=(int)$data['user_id'];
        }else{
            $user_id=0;
        }
        $type=(int)$data['type'];//类型->0:所有任务，1：本人的所有任务，2：处理人能看到的任务，3：验证人能看到的任务
        $status=(int)$data['status'];
        $page=(int)$data['page'];
        $size=(int)$data['size'];
        $start=($page-1)*$size;
        $start_time=(int)$data['start_time'];
        $end_time=(int)$data['end_time'];

        $where=' where is_delete=1 ';
        switch ($type){
            case 0://所有任务
                break;
            case 1://本人任务
                if($user_id!=0){
                    $where.=" and up_user_id=$user_id";
                }
                break;
            case 2://处理人任务
                if($user_id!=0){
                    $where.=" and deal_user_id=$user_id";
                }
                break;
            case 3://验证人任务
                if($user_id!=0){
                    $where.=" and check_user_id=$user_id";
                }
                break;
            default:
                mysqli_close($con);
                return out_error('非法访问！');
                break;
        }
        if($start_time!=0){
            $where.=" and create_time between $start_time and $end_time";
        }
        if($status!=100){
            $where.=" and status=$status";
        }

        $limit=" order by update_time DESC limit $start,$size";

        $sql="select * from task".$where;
        $data=mysqli_query($con,$sql.$limit);
        $data=mysqli_fetch_all($data,true);
        $total=mysqli_query($con,$sql);
        $total=mysqli_num_rows($total);

        if(count($data)!=0){
            $user_ids=array_merge(array_column($data,'up_user_id'),array_column($data,'deal_user_id'),array_column($data,'check_user_id'));
            $a=$user_ids;
            for($i=0;$i<count($a);$i++){
                if(empty($a[$i])){
                    unset($user_ids[$i]);
                }
            }
            sort($user_ids);
            $user_ids=implode(',',$user_ids);
            $user=mysqli_query($con,"select * from user where id in ($user_ids)");
            $user=mysqli_fetch_all($user,true);
            for($i=0;$i<count($data);$i++){
                $data[$i]['up_user_name']='';
                $data[$i]['up_user_phone']='';
                $data[$i]['deal_user_name']='';
                $data[$i]['deal_user_phone']='';
                $data[$i]['check_user_name']='';
                $data[$i]['check_user_phone']='';
                if(!empty($data[$i]['img_url'])){//任务图片集合
                    $data[$i]['img_url']=explode(',',$data[$i]['img_url']);
                }else{
                    $data[$i]['img_url']=[];
                }
                if(!empty($data[$i]['deal_img'])){//执行图片集合
                    $data[$i]['deal_img']=explode(',',$data[$i]['deal_img']);
                }else{
                    $data[$i]['deal_img']=[];
                }
                if(!empty($data[$i]['pre_time'])){//预约时间
                    $data[$i]['pre_time']=date('Y-m-d H:i:s',$data[$i]['pre_time']);
                }else{
                    $data[$i]['pre_time']='';
                }
                if(!empty($data[$i]['band_time'])){//领用时间
                    $data[$i]['band_time']=date('Y-m-d H:i:s',$data[$i]['band_time']);
                }else{
                    $data[$i]['band_time']='';
                }
                if(!empty($data[$i]['server_time'])){//上门服务时间
                    $data[$i]['server_time']=date('Y-m-d H:i:s',$data[$i]['server_time']);
                }else{
                    $data[$i]['server_time']='';
                }
                if(!empty($data[$i]['check_time'])){//验证时间
                    $data[$i]['check_time']=date('Y-m-d H:i:s',$data[$i]['check_time']);
                }else{
                    $data[$i]['check_time']='';
                }
                if(!empty($data[$i]['cancel_time'])){//取消时间
                    $data[$i]['cancel_time']=date('Y-m-d H:i:s',$data[$i]['cancel_time']);
                }else{
                    $data[$i]['cancel_time']='';
                }
                if(!empty($data[$i]['create_time'])){//创建时间
                    $data[$i]['create_time']=date('Y-m-d H:i:s',$data[$i]['create_time']);
                }else{
                    $data[$i]['create_time']='';
                }
                if(!empty($data[$i]['update_time'])){//修改时间
                    $data[$i]['update_time']=date('Y-m-d H:i:s',$data[$i]['update_time']);
                }else{
                    $data[$i]['update_time']='';
                }
                if(empty($data[$i]['check_remark'])){//验证评价
                    $data[$i]['check_remark']='';
                }
                for($o=0;$o<count($user);$o++){
                    if($data[$i]['up_user_id']==$user[$o]['id']){//发布人
                        $data[$i]['up_user_name']=$user[$o]['name'];
                        $data[$i]['up_user_phone']=$user[$o]['phone'];
                    }
                    if($data[$i]['deal_user_id']==$user[$o]['id']){//执行人
                        $data[$i]['deal_user_name']=$user[$o]['name'];
                        $data[$i]['deal_user_phone']=$user[$o]['phone'];
                    }
                    if($data[$i]['check_user_id']==$user[$o]['id']){//验证人
                        $data[$i]['check_user_name']=$user[$o]['name'];
                        $data[$i]['check_user_phone']=$user[$o]['phone'];
                    }
                }
            }
        }
        mysqli_close($con);
        return out_right([
            'data'=>$data,
            'page'=>$page,
            'size'=>$size,
            'total'=>$total,
            'total_page'=>ceil($total/$size)
        ]);
    }

    //取消任务
    public function cancel($data){
        $id=(int)$data['id'];

        $con=con();
        $time=time();
        $res=mysqli_query($con,"update task set status=-1,cancel_time=$time,update_time=$time where id=$id and status=0");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error('任务取消失败！');
        }
    }

    //领取任务
    public function getTask($data){
        $id=(int)$data['id'];
        $user_id=(int)$data['user_id'];

        $con=con();
        $time=time();
        //取消权限控制
        $res=mysqli_query($con,"select * from user_power_config where user_id=$user_id and type=3 and status=1 and is_delete=1 and check_status=1");
        $res=mysqli_num_rows($res);
        if($res!=1){
            mysqli_close($con);
            return out_error('无权操作！');
        }
        $res=mysqli_query($con,"update task set status=1,deal_user_id=$user_id,band_time=$time,update_time=$time where id=$id and status=0");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }

    }

    //完成任务
    public function finishTask($data){
        $id=(int)$data['id'];
        $user_id=(int)$data['user_id'];
        $content=$data['content'];
        $img=$data['img'];
        $date=date('Y-m-d');
        $url=[];


        $con=con();
        $time=time();
        //取消权限控制
        $res=mysqli_query($con,"select * from user_power_config where user_id=$user_id and type=3 and status=1 and is_delete=1 and check_status=1");
        $res=mysqli_num_rows($res);
        if($res!=1){
            mysqli_close($con);
            return out_error('无权操作！');
        }

        //处理图片
        for($i=0;$i<count($img);$i++){
            $file=base64_to_file::get("",$img[$i]["file_name"],$img[$i]["file"]);
            $res=oss::upload("task/$date/".$file["name"],$file["path"]);
            $url[]=$res["info"]["url"];
        }
        $url=implode(',',$url);

        $res=mysqli_query($con,"update task set status=2,deal_content='$content',deal_img='$url',server_time=$time,update_time=$time where id=$id and status=1");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }

    }

    //验证任务
    public function checkTask($data){
        $id=(int)$data['id'];
        $user_id=(int)$data['user_id'];
        $check_remark=$data['check_remark'];

        $con=con();
        $time=time();
        //取消权限控制
        $res=mysqli_query($con,"select * from user_power_config where user_id=$user_id and type=4 and status=1 and is_delete=1 and check_status=1");
        $res=mysqli_num_rows($res);
        if($res!=1){
            mysqli_close($con);
            return out_error('无权操作！');
        }
        $res=mysqli_query($con,"update task set status=3,check_time=$time,update_time=$time,check_remark='$check_remark',check_user_id=$user_id where id=$id and status=2");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }

    }

    //驳回任务
    public function refuseTask($data){
        $id=(int)$data['id'];
        $user_id=(int)$data['user_id'];
        $check_remark=$data['check_remark'];

        $con=con();
        $time=time();
        //取消权限控制
        $res=mysqli_query($con,"select * from user_power_config where user_id=$user_id and type=4 and status=1 and is_delete=1 and check_status=1");
        $res=mysqli_num_rows($res);
        if($res!=1){
            mysqli_close($con);
            return out_error('无权操作！');
        }
        $res=mysqli_query($con,"update task set status=1,check_time=$time,update_time=$time,check_remark='$check_remark',check_user_id=$user_id where id=$id and status=2");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }

    }

}